<?php
class coupon extends admin
{
    public function __construct(){
        parent::__construct();
    }
    public function pt_add(){
        if(isset($_POST['status'])&&$_POST['status']==1) $date['status']=1;
        else $date['status']=0;
        $date['name'] = trim($_POST['name']);
        $date['money']  = $_POST['money'];
        $date['beizhu'] = trim($_POST['beizhu']);
        $date['begin_time'] = strtotime($_POST['begin_time']);
        $date['end_time'] = strtotime($_POST['begin_time']);
        $date['begin_date'] = strtotime($_POST['begin_date']);
        $date['end_date'] = strtotime($_POST['end_date']);
        $date['school_id'] = $_SESSION['client']['jx_id'];
        $rs = db()->insert('x_youhq_type', $date);
        if($rs){
        insertlogs('create','添加优惠券信息(ID为:'.$rs.')',$date,$rs);
        $return = array('status'=>1,'info'=>'添加完成');
        }else{
            $return = array('status'=>2,'info'=>'添加失败','date'=>$_POST);
        }
        echo json_encode($return);
    }
    public function modify(){
        if(!$_POST['name']){
        $id= $_POST['id'];
        $rs = db()->getRow("select id,name,money,begin_time,begin_date,end_date,status,beizhu from x_youhq_type WHERE id=".$id);
        if(!$rs) $rs = array();
        echo json_encode(array('status'=>1,'info'=>$rs));
        }else{
            $id = intval($_POST['id']);
            $date['begin_time'] = strtotime($_POST['begin_time']);
            $date['begin_date'] = strtotime($_POST['begin_date']);
            $date['end_date'] = strtotime($_POST['end_date']);
            $date['beizhu']     = trim($_POST['beizhu']);
            $date['name']       = trim($_POST['name']);
            $date['money']      = $_POST['money'];
            $date['status']     = $_POST['status'];
            $rs = db()->update('x_youhq_type',$date,'id='.$id);
            insertlogs('update','修改优惠券信息(ID:'.$id.')',$date,$id);
            if($rs !==false){
              $return = array('status'=>1,'info'=>'更新成功');
            }else{
              $return = array('status'=>0,'info'=>'系统繁忙,请稍后重试');
            }
            echo json_encode($return);
        }

    }
    public function select(){
        $where = " where  1=1 and school_id='" . $_SESSION['client']['jx_id'] . "'";
        if($_GET['search']) {
            $search = trim($_GET['search']);
            $where.= " and name like '%".$search."%' ";
        }
        $total = db()->getOne("select count(*) from x_youhq_type".$where);
        $pagesize=10;
        if (isset($_GET['p']) && (int) $_GET['p'] > 0) $p = (int) $_GET['p'];
        else $p = 1;
        $limit=' limit '.($p-1)*$pagesize.','.$pagesize;
        $rs=db()->getAll("select id,name,money,begin_time,end_time,period,status,type,beizhu from x_youhq_type".$where." $limit");
        $page = array('total' => $total,'pagesize' =>$pagesize,'curpage' => $p);
        foreach ($rs as $k =>$v){
            $rs[$k]['begin_time'] = date('Y-m-d H:i:s',$v['begin_time']);
            $rs[$k]['end_time']   = date('Y-m-d H:i:s',$v['end_time']);
            $rs[$k]['lq_num']   = db()->getOne("select count(*) from x_youhuiquan WHERE type_id = ".$v['id']) .'人';
            $rs[$k]['sy_num']   = db()->getOne("select count(*) from x_youhuiquan WHERE status=1 and  type_id = ".$v['id']) .'人';
            $rs[$k]['sos']  = round(($rs[$k]['sy_num']/$rs[$k]['lq_num'])*100).'%';
        }
        echo json_encode(array('status'=>1,'info'=>$rs,'page'=>$page));
    }
    //优惠券列表:
    public function index()
    {
        if (IS_AJAX) {
            $where = ' where 1=1';
            if ($_GET['couponAmount'] != '') {
                $where .= ' and money="' . $_GET['couponAmount'] . '"';
            }
            if (!empty($_GET['couponName'])) {
                $where .= ' and name like"%' . $_GET['couponName'] . '%"';
            }
            $sql1 = "select id,name,money,period,begin_time,end_time,begin_date,end_date,status,type,beizhu from x_youhq_type" . $where;
            $x_youhq_type = db()->getAll($sql1);
            foreach ($x_youhq_type as $k => $v) {
                $x_youhq_type[$k]['begin_date'] = date("Y-m-d", $v['begin_date']);
                $x_youhq_type[$k]['end_date'] = date("Y-m-d", $v['end_date']);
                $x_youhq_type[$k]['begin_time'] = date("Y-m-d H:i:s", $v['begin_time']);
                $x_youhq_type[$k]['end_time'] = date("Y-m-d H:i:s", $v['end_time']);
                if ($v['status'] == '1') {
                    $x_youhq_type[$k]['status'] = '已启用';
                } elseif ($v['status'] == '0') {
                    $x_youhq_type[$k]['status'] = '未启用';
                }
            }
            $this->ajaxReturn(array('status' => 1, 'data' => $x_youhq_type));
        }else {
            $tpl = $this->Template();
            $tpl->display();
        }

    }

    //优惠券添加:
public function couponAdd()
{
    if (IS_AJAX && IS_POST) {
        if (empty($_POST)) {
            $this->ajaxReturn(array('status' => 'n', 'info' => '数据未接受'));
        }
        if(isset($_POST['status'])&&$_POST['status']==1) $date['status']=1;
        else $date['status']=0;
        $date['type'] = $_POST['type'];
        $date['name'] = $_POST['name'];
        $date['money'] = $_POST['money'];
        $begin_date = strtotime($_POST['begin_date']);
        $end_date = strtotime($_POST['end_date']);
        $date['begin_time'] = strtotime($_POST['begin_time']);
        $date['end_time']   = strtotime($_POST['end_time']);
        $date['begin_date'] = $begin_date;
        $date['end_date'] = $end_date;
        $date['beizhu'] = $_POST['beizhu'];
        $youhqList = db()->insert('x_youhq_type', $date);
        if ($youhqList){
            $userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
            $logs = json_encode($date);
            $infoid = $youhqList;
            $method = 'create';
            //            $sub = substr($_SERVER['REDIRECT_URL'],'1');
            //            $mid = db()->getOne("SELECT id FROM x_menu WHERE path='" . $sub . "'");
            $mid = '77';
            $remark = '添加优惠券类型ID为'.$youhqList;
            insertlogs($userData,$logs,$infoid,$method,$mid,$remark);
             $this->ajaxReturn(array('status' => 'y', 'info' => '数据添加成功'));
        }
        else $this->ajaxReturn(array('status' => 'n', 'info' => '操作失败'));
    } else {
        $tpl = $this->Template();
        $tpl->display('coupon/couponAdd.html');
    }
}

public function edit()
{
    $tpl = $this->Template();
    if(IS_AJAX&&IS_POST){
        if(empty($_POST['id'])) $this->ajaxReturn(array('status'=>'n','info'=>'ID不可用'));
        $id = $_POST['id'];
        if(isset($_POST['status'])&&$_POST['status']==1) $date['status']=1;
        else $date['status']=0;
        $date['money'] = $_POST['money'];
        $date['type']  = $_POST['type'];
        $date['begin_date'] = strtotime($_POST['begin_date']);
        $date['end_date']   = strtotime($_POST['end_date']);
        $date['begin_time']   = strtotime($_POST['begin_time']);
        $date['end_time']   = strtotime($_POST['end_time']);
        $date['beizhu']     = $_POST['beizhu'];
        $editResult = db()->update('x_youhq_type',$date,'id='.$id);
        if($editResult!==false){
            $userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
            $logs = json_encode($date);
            $infoid = $id;
            $method = 'update';
//            $sub = substr($_SERVER['REDIRECT_URL'],'1');
//            $mid = db()->getOne("SELECT id FROM x_menu WHERE path='" . $sub . "'");
            $mid = '78';
            $remark = '添加优惠券类型ID为'.$id;
            insertlogs($userData,$logs,$infoid,$method,$mid,$remark);
            $this->ajaxReturn(array('status'=>'y','info'=>'更新成功'));
        }else{
            $this->ajaxReturn(array('status'=>'n','info'=>'更新失败'));
        }
    }else{
        if(empty($_GET['youhqid']))$this->ajaxReturn(array('status'=>'n','info'=>'ID不可用'));
        $youhqid = $_GET['youhqid'];
        $row = db()->getRow("select id,name,money,begin_time,end_time,begin_date,status,type,end_date,period,beizhu from x_youhq_type WHERE id='$youhqid'");
        $row['begin_date'] = date("Y-m-d H:i:s",$row['begin_date']);
        $row['end_date'] = date("Y-m-d H:i:s",$row['end_date']);
        $row['begin_time'] = date("Y-m-d H:i:s",$row['begin_time']);
        $row['end_time'] = date("Y-m-d H:i:s",$row['end_time']);
        $tpl->assign('row',$row);
        $tpl->display('coupon/edit.html');
    }
}
public function del()
{
$id = $_GET['id'];
$delResult = db()->delete('x_youhq_type','id='.$id);
    if($delResult){
        $this->ajaxReturn(array('status'=>'y','info'=>'删除成功'));
    }else{
        $this->ajaxReturn(array('status'=>'n','info'=>'删除失败'));
    }
}
}







